{extend name="admin/admin" /}

{block name="menu"}

<!--添加角色模态框-->

<div id="addrole" style="margin-top:150px;" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog"
     aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <h1 style="text-align: center; font-size:20px;color:white; ">分配角色</h1>
        <input type="hidden" id="id" name="id">


        <div style="color:white;font-size: 20px;" class="form-group">
            <input type="hidden" name="id" value="">
            {volist name="role" key="k" id="v" }

            <input type="checkbox" name="role" value="{$v['id']}">{$v['name']}

            {/volist}

        </div>

        <button type="button" style="margin-top: 30px;margin-left: 400px;"
                onclick="dorole()" class="btn btn-success">提交
        </button>
    </div>
</div>

<!--查看角色模态框-->

<div id="roleall" style="margin-top:150px;" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog"
     aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <h1 style="text-align: center; font-size:20px;color:white; "></h1>


        <div id="box" style="color:white;font-size: 20px;" class="form-group">


        </div>


    </div>
</div>
<!--end模态框-->
<h1 style="text-align: center; font-size:50px;">管理员分配角色</h1>

<div class="table-responsive">
    <table class="table" id="table2">
        <tr>
            <th width="5%" class="active">编号</th>
            <th width="25%" class="success">用户名</th>
            <th width="45%" class="info">分配</th>

        </tr>
        {volist name="rows" key="k" id="v" }
        <tr>
            <td class="active">{$v['id']}</td>
            <td class="warning">{$v['name']}</td>
            <td class="info">
                <button onclick="add(this)" data-toggle="modal" data-target="#addrole" class="btn btn-success"> 分配角色
                </button>

                <button onclick="roleall(this)" data-toggle="modal" data-target="#roleall" class="btn btn-info"> 查看角色
                </button>
            </td>

        </tr>
        {/volist}
    </table>


</div>

<script>

    function roleall(c) {

        var admin_id = $(c).parents('tr').find("td:eq(0)").text();
        $.ajax({
            method: 'post',
            url: "{:url('admin/admin/roleall')}",
            data: {
                "id": admin_id,
            },
            dataType: 'json',
            success: function (data) {
                $('#box').html('');
                for (var i = 0; i < data.length; i++) {
                    $('#box').append(data[i]['name'] + ' ');
                }

            },
            error: function () {
            }
        });
    }


    function add(b) {
        id = $(b).parents('tr').find("td:eq(0)").text();
        // $("input[ name='id']").val(id);

    }


    //分配角色
    function dorole() {
        var roles = [];
        $('input[name="role"]:checked').each(function () {
            roles.push($(this).val());
        });
        if (roles.length == 0) {
            alert('操作失败,请重试');
            return false;
        }

        $.ajax({
            method: 'post',
            url: "{:url('admin/admin/dorole')}",
            data: {
                "roles": roles,
                "id": id,
            },
            dataType: 'json',
            success: function (data) {
                if (data == 1) {
                    alert('添加成功');
                    location.reload();
                } else {
                    alert('添加有误,请重试');
                    location.reload();
                }
            },
            error: function () {
                alert('操作完成');
                location.reload();

            }
        });
    }


</script>
{/block}
